<template>
  <div style="height: 100%">
    <AHMF
      :asideWidth="300"
      :rightTitleShow="false"
      :AShow="false"
      leftTitle="模板类型"
    >
      <template #header>
        <topSearcher
          :searchOptions="searchOptions"
          @onQuery="onQuery"
          @onReset="onReset"
          :border="false"
        ></topSearcher>
      </template>
      <template #main>
        <div style="margin-bottom: 10px; display: flex">
          <el-button type="primary" size="small" @click="handleFn('add')"
            >新增</el-button
          >
        </div>

        <ETable
          :data="tableData"
          :titleObj="tableColumns"
          operateWidth="240"
          :loading="tableLoading"
        >
          <template v-slot="{ scope }">
            <el-button @click="handleFn('edit', scope)" type="text" size="small"
              >修改</el-button
            >
            <el-button
              @click="uploadFile('detail', scope)"
              type="text"
              size="small"
              >详情</el-button
            >
            <el-button
              @click="uploadFile('upload', scope)"
              type="text"
              size="small"
              >上传文件</el-button
            >
            <el-button @click="delFn(scope)" type="text" size="small"
              >删除</el-button
            >
          </template>
        </ETable>
      </template>
      <template #footer>
        <div class="flex">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageInfo.currPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageInfo.total"
          >
          </el-pagination>
        </div>
      </template>
    </AHMF>
    <add_edit_dialog
      ref="add_edit_dialog"
      @refreshFn="onQuery"
    ></add_edit_dialog>
    <upload_dialog ref="upload_dialog" @refreshFn="onQuery"></upload_dialog>
  </div>
</template>
<script>
import topSearcher from "@/components/topSearcher/topSearcher.vue";
import AHMF from "@/components/topSearcher/AHMF.vue";
import ETable from "@/components/ETable";
import add_edit_dialog from "./components/add_edit_dialog.vue";
import upload_dialog from "./components/upload_dialog.vue";
import {
  BsTemplateApi,
  BsTemplateByIdApi,
  deleteBsTemplateByIds,
  postBsTemplateApi,
  putBsTemplateApi,
  saveWithFileApi,
} from "@/api/templateManage";
export default {
  components: {
    AHMF,
    topSearcher,
    ETable,
    add_edit_dialog,
    upload_dialog,
  },

  data() {
    return {
      //表格字段
      tableColumns: [
        {
          prop: "templateCode",
          label: "模板编码",
        },
        {
          prop: "templateName",
          label: "模板名称",
        },
        { prop: "subjectNum", label: "科目数量" },
        { prop: "editorSubjectNum", label: "编辑数量" },
        { prop: "crMem", label: "创建人" },
        { prop: "remark", label: "备注" },
      ],
      // 搜索字段
      searchOptions: [
        {
          type: "input",
          label: "模板名称",
          key: "templateName",
          value: "",
        },
      ],
      searchForm: {
        templateName: "",
      },
      tableData: [],
      tableLoading: false,
      pageInfo: {
        currPage: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  mounted() {
    // this.getTableData();
  },
  methods: {
    // 删除
    async delFn(scope) {
      let t = await this.$confirm("确认删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      });
      if (t) {
        let res = await deleteBsTemplateByIds(scope.id);
        if (res.data && res.data.bcode == 0) {
          // 页数大于一页并且当前页只有一条数据，删除后页数-1
          if (
            this.pageInfo.total % this.pageInfo.pageSize === 1 &&
            this.pageInfo.currPage > 1
          ) {
            this.pageInfo.currPage -= 1;
          }
          this.$message.success(res.data.bmsg);
          this.onQuery();
        }
      }
    },

    uploadFile(type, scope) {
      this.$refs.upload_dialog.openFn(type, scope);
    },
    handleFn(type, row) {
      this.$refs.add_edit_dialog.openFn(type, row);
    },

    // 获取表格数据
    async getTableData() {
      this.tableLoading = true;
      // 没有排序条件
      let obj = {
        currPage: this.pageInfo.currPage,
        pageSize: this.pageInfo.pageSize,
        ...this.searchForm,
      };

      let res = await BsTemplateApi(obj);
      if (res.data && res.data.bdata.total > 0) {
        this.tableData = res.data.bdata.gdata;
        this.pageInfo.total = res.data.bdata.total;
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.tableLoading = false;
    },
    onReset(searchForm) {
      console.log("重置", searchForm);
      // this.searchForm = searchForm;
      this.pageInfo.currPage = 1;
      this.onQuery(searchForm);
    },
    onQuery(searchForm) {
      console.log("查询", searchForm);
      this.searchForm = searchForm;
      this.getTableData();
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageInfo.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageInfo.currPage = val;
      this.getTableData();
    },
    onSubmit() {
      console.log("submit!");
    },
    handleNodeClick(data) {
      console.log(data);
    },
    handleClick(row) {
      console.log(row);
    },
    //树节点筛选
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
  },
};
</script>
<style lang="scss" scope></style>
